package de.vocabulary.client.editor;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.user.client.ui.FlexTable;

import de.vocabulary.client.LanguageModuleManager;
import de.vocabulary.shared.model.LanguageModule;
import de.vocabulary.shared.model.Vocable;

public class VocableTable extends FlexTable {
	
	private List<String> header;
	private List<Vocable> list;
	private List<VocableDriver> driver;
	private List<VocableEditor> editors;
	private boolean verticalEditing;
	
	interface VocableDriver extends SimpleBeanEditorDriver<Vocable, VocableEditor> {}
	
	private LanguageModule languageModule;
	

	
	public VocableTable() {
		super();
		this.setCellPadding(1);
		this.setCellSpacing(0);
		this.setWidth("100%");
		
		languageModule = LanguageModuleManager.getLanguageModuleManager().getLanguageModule();
		
		header = new ArrayList<String>();
	    header.add(languageModule.getSourceLanguage());
	    header.add(languageModule.getTargetLanguage());
	    list = new ArrayList<Vocable>();
	    driver = new ArrayList<VocableDriver>();
	    editors = new ArrayList<VocableEditor>();
	    verticalEditing = languageModule.isVerticalEditMode();
	    
	}
	
	
	
	public List<Vocable> getStoredVocabulary(String lesson){
		for (int i=0; i<driver.size(); i++){
			driver.get(i).flush();
			list.get(i).setLanguage2Word(editors.get(i).language2Word.getText());
			list.get(i).setLesson(lesson);
		}
		
		return list;
	}

	
	
	public void addNewVocables(int amount){
		if (amount <= 0) {
			return;
		}
		for (int i=0; i<amount; i++) {
			addVocable(new Vocable());
			
		}
	}
	
	
	

	public void addVocables(List<Vocable> input) {
		for (int i = this.getRowCount(); i > 0; i--) {
			this.removeRow(0);
		}
		if (input == null) {
			return;
		}

		int row = 0;
		if (header != null) {
			int i = 0;
			for (String string : header) {
				this.setText(row, i, string);
				i++;
			}
			row++;
		}
		
		// Make the table header look nicer
		this.getRowFormatter().addStyleName(0, "tableHeader");

		for (Vocable voc : input) {
			addVocable(voc);
		}
	}
	
	public void updateTabIndex(boolean verticalEditing){
		System.out.println("Updating vertial editing: "+verticalEditing);
		int i=0;
		for (VocableEditor e:editors){
			e.setTabIndex(i,verticalEditing);
			i++;
		}
		this.verticalEditing = verticalEditing;
	}

	public void addVocable(Vocable voc) {
		list.add(voc);
		VocableEditor e = new VocableEditor();
		VocableDriver editorDriver = GWT.create(VocableDriver.class);
		driver.add(editorDriver);
		editors.add(e);
		
		editorDriver.initialize(e);
		editorDriver.edit(voc);
		e.setTabIndex(this.getRowCount(),verticalEditing);
		this.setWidget(this.getRowCount(), 0, e);
	}



	public void setTranslations(List<List<String>> result) {
		for (int i=0; i<result.size(); i++){
			if (result.get(i)!=null){
				editors.get(i).setSuggestions(result.get(i));
			}
		}
		
	}
	
	public void removeVocable(VocableEditor editor){
		int index = editors.indexOf(editor);
		System.out.println("Removing index: "+index);
		this.remove(editors.get(index));
		list.remove(index);
		driver.remove(index);
		editors.remove(index);
		list.remove(editor);
	}
	
	public void gotoNextField(boolean language1WordSelected, VocableEditor e){
		int index = editors.indexOf(e);
		if (verticalEditing){
			if (index==editors.size()-1){
				addVocable(new Vocable());
			}
			editors.get(index+1).language1Word.setFocus(true);
		}else{
			if (!language1WordSelected){
				if (index==editors.size()-1)
					addVocable(new Vocable());
				editors.get(index+1).language1Word.setFocus(true);
			}else
				editors.get(index).language2Word.setFocus(true);
			
		}
	}



	public void goUp(boolean language1WordSelected, VocableEditor e) {
		int index = editors.indexOf(e);
		if (index-1<0) return;
		if (language1WordSelected)
			editors.get(index-1).language1Word.setFocus(true);
		else
			editors.get(index-1).language2Word.setFocus(true);
	}
	
	public void goDown(boolean language1WordSelected, VocableEditor e) {
		int index = editors.indexOf(e);
		if (index+1>editors.size()-1) return;
		if (language1WordSelected)
			editors.get(index+1).language1Word.setFocus(true);
		else
			editors.get(index+1).language2Word.setFocus(true);
	}
	
	public void goLeft(boolean language1WordSelected, VocableEditor e) {
		int index = editors.indexOf(e);
		if (language1WordSelected) return;
		else
			editors.get(index).language1Word.setFocus(true);
	}
	
	public void goRight(boolean language1WordSelected, VocableEditor e) {
		int index = editors.indexOf(e);
		if (!language1WordSelected) return;
		else
			editors.get(index).language2Word.setFocus(true);
		
	}

}
